package com.wish.housing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.housing.entity.*;
import com.wish.housing.entity.centralization.Louceng;
import com.wish.housing.entity.centralization.Loudong;
import com.wish.housing.entity.centralization.Room;
import com.wish.housing.es.EsRoomRinfoRepository;
import com.wish.housing.mapper.TbRoomInfoMapper;
import com.wish.housing.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 王泓翔-房间详细信息表	！房态-杨子强 服务实现类
 * </p>
 *
 * @author gaoluwen
 * @since 2022-03-29
 */
@Service
public class TbRoomInfoServiceImpl extends ServiceImpl<TbRoomInfoMapper, TbRoomInfo> implements ITbRoomInfoService {
    @Autowired
    ITbRelationRoomPictureService iTbRelationRoomPictureService;
    @Autowired
    ITbRelationHousePictureService iTbRelationHousePictureService;
    @Autowired
    ITbHouseStyleService iTbHouseStyleService;
    @Autowired
    ITbRelationStylePictureService iTbRelationStylePictureService;
    @Autowired
    EsTbRoomInfoService esTbRoomInfoService;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    EsRoomRinfoRepository esRoomRinfoRepository;
    @Override
    public void saveTbRoomReservation(TbRoomInfo room, List<String> houseImages,List<String> roomImages) {
        Date date = new Date();
        room.setId(UUID.randomUUID().toString());
        room.setVacancyDate(date);
        room.setCreateTime(date);
        room.setRecommended(2);
        room.setRoomCode("01");
        baseMapper.saveTbRoomInfo(room);
        //es增量同步
        EsRoomInfo roomInfoById = esTbRoomInfoService.findRoomInfoById(room.getId());
        esRoomRinfoRepository.save(roomInfoById);
        if(roomImages != null && roomImages.size()>0){
            for (String roomImage : roomImages) {
                //添加 tb_relation_room_picture
                iTbRelationRoomPictureService.save(new TbRelationRoomPicture(room.getId(),roomImage));
            }
        }
        if(houseImages !=null && houseImages.size()>0){
            for (String houseImage : houseImages) {
                //添加 tb_relation_room_picture
                iTbRelationRoomPictureService.save(new TbRelationRoomPicture(room.getId(),houseImage));
            }
        }
    }

    @Override
    public void                                                                                        saveCentralLized(List<Loudong> loudongs) {
        String houseId = null;
        for (Loudong loudong : loudongs) {
            houseId = loudong.getHouseId();
            break;
        }
        //获取tb_relation_house_picture中的图片
        List<String> houseImages = iTbRelationHousePictureService.getRoomPictureByHouseId(houseId);
        boolean delRoomPicFlag = true;
        //根据houseId删除tb_relation_room_picture下房源的图片
        iTbRelationRoomPictureService.deleteRoomPicture(houseId);
        //根据houseId 删除tb_room_info中的数据
        baseMapper.deleteRoomInfo(houseId);
        for (int i = 0; i < loudongs.size(); i++) {
            //楼栋
            RoomInfo roomInfo = new RoomInfo();
            Loudong loudong = loudongs.get(i);
            if(loudong.getHeight()==null){
                roomInfo.setFloorCount(0);
            }else{
                roomInfo.setFloorCount(Integer.parseInt(loudong.getHeight()));
            }
            roomInfo.setBuildingName(loudong.getName());
            //楼层
            List<Louceng> loucengs = loudongs.get(i).getLouceng();
            for (int j = 0; j < loucengs.size(); j++) {
                Louceng louceng = loucengs.get(j);
                roomInfo.setFloor(louceng.getLoucengNum());
                //房间
                List<Room> roomArr = loucengs.get(j).getRoomArr();
                for (int k = 0; k < roomArr.size(); k++) {
                    Room room = roomArr.get(k);
                    roomInfo.setRoomCode(room.getNum());
                    roomInfo.setRoomName(room.getFlatName()+" "+room.getRoom()+" 室 "+room.getHall()+" 厅 "+room.getNum());
                    roomInfo.setHouseId(room.getHouseId());
                    roomInfo.setCreateTime(new Date());
                    roomInfo.setRecommended(2);
                    if(room.isChuzu()){
                        roomInfo.setStatus(1);
                    }else{
                        roomInfo.setStatus(0);
                    }
                    //根据style的id 获取 tb_house_style
                    TbHouseStyle houseStyle = iTbHouseStyleService.getHouseStyle(room.getType());
                    //根据style的id 获取StylePicture
                    String houseStyleImage = iTbRelationStylePictureService.getStylePicture(room.getType());
                    if(houseStyle!=null){
                        roomInfo.setProportion(houseStyle.getProportion());
                        roomInfo.setDirection(houseStyle.getDirection());
                        roomInfo.setRental(houseStyle.getRental());
                        roomInfo.setDecorateStyle(houseStyle.getDecorateStyle());
                        roomInfo.setPrivateFacility(houseStyle.getPrivateFacility());
                        roomInfo.setStyleId(room.getType());
                    }
                    if(!"".equals(houseStyleImage) && houseStyleImage!=null && houseStyleImage.length() > 0 && houseStyleImage.trim().length() >0 ){
                        roomInfo.setFrontCover(houseStyleImage);
                    }
                    String roomId = null;
                    if(room.getRoomId()!=null){
                        roomId = room.getRoomId();
                        roomInfo.setId(roomId);
                        delRoomPicFlag = true; //更新集中式房源的图片
                    }else{
                        roomId = UUID.randomUUID().toString();
                        roomInfo.setId(roomId);
                    }
                    baseMapper.saveRoomInfo(roomInfo);
                    //根据房间设置的房型 查找房型图片
                    List<String> roomStyleImages = iTbRelationStylePictureService.getRoomStylrImageByStyleId(room.getType());
                    for (String roomStyleImage : roomStyleImages) {
                        iTbRelationRoomPictureService.saveRoomPicture(roomId,roomStyleImage);
                    }
                    if(houseImages != null && houseImages.size() >0){
                        for (String houseImage : houseImages) {
                            iTbRelationRoomPictureService.saveRoomPicture(roomId,houseImage);
                        }
                    }
                }
            }
        }
    }
}
